package com.ln.asset.core.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.ln.asset.common.result.R;
import com.ln.asset.core.mapper.InsAddMapper;
import com.ln.asset.core.mapper.OmMapper;
import com.ln.asset.core.pojo.InsAdd;
import com.ln.asset.core.pojo.NjSub;
import com.ln.asset.core.mapper.NjSubMapper;
import com.ln.asset.core.pojo.Om;
import com.ln.asset.core.service.InsAddService;
import com.ln.asset.core.service.NjSubService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author cuixiaoxin
 * @since 2022-04-29
 */
@Service
public class NjSubServiceImpl extends ServiceImpl<NjSubMapper, NjSub> implements NjSubService {
    @Resource
    private OmMapper omMapper;

    @Resource
    private InsAddService insAddService;

    @Resource
    private NjSubMapper njSubMapper;

    @Override
    public Integer addAssetcj(NjSub njSub) {
        Integer num = baseMapper.insert(njSub);
        return num;
    }

    @Override
    public Integer UpdateByNjSub(Integer suid, Integer sustart) {
      SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        //根据申购单编号查询所有的信息
        List<Om> oms = omMapper.selectOmById(suid);
        Date date = new Date();
        System.out.println("time         " + date);
        NjSub njSub = new NjSub();
        njSub.setSuid(suid);
        njSub.setSustart(sustart);
        String nuu1=sdf.format(date);
        njSub.setSuexamine(nuu1);
        //修改申购表中的状态 待完善……
        Integer num = this.UpdateByNjSubSGByID(njSub, sustart);
        if (num > 0) {
            List<Integer> suids = new ArrayList<>();
            for (int i = 0; i < oms.size(); i++) {
                suids.add(oms.get(i).getAdid());
            }
            //修改用品表的所有状态
            for (int j = 0; j < suids.size(); j++) {
                InsAdd insAdd = new InsAdd();
                insAdd.setAdid(suids.get(j));
                insAdd.setInjstart(sustart);
                System.out.println(insAdd.getInjstart());
                num = insAddService.updateAssetCj(insAdd);
            }
        }

        return num;
    }

    @Override
    public Integer UpdateByNjSubBank(Integer suid, Integer subank) {
        SimpleDateFormat sdf =new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        Integer num = 0;
        NjSub njSub = new NjSub();
        njSub.setSuid(suid);
        njSub.setSubank(subank);
        Date date = new Date();
        String numm=sdf.format(date);
        System.out.println("------------------   "+numm);
        njSub.setSulib(numm);
        //修改申购中的入库状态
        num = njSubMapper.updateByIdNjSubRK(njSub, subank);
        return num;
    }

    @Override
    public Integer UpdateByNjSubBankCG(Integer suid, Integer supustart) {
        SimpleDateFormat sdf =new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        Integer num = 0;
        NjSub njSub = new NjSub();
        njSub.setSuid(suid);
        njSub.setSupustart(supustart);
        Date date = new Date();
        String nuu1=sdf.format(date);
        njSub.setSupur(nuu1);
        //修改申购中的采购状态
        num = njSubMapper.updateByIdNjSubCG(njSub, supustart);
        return num;
    }


    @Override
    public Integer UpdateByNjSubSGByID(NjSub njSub, Integer sustarts) {
        Integer num = njSubMapper.UpdateNjByIdSG(njSub, sustarts);
        return num;
    }

    @Override
    public Integer UpdateByNjSubBankJY(Integer suid, Integer sucheckout) {
        SimpleDateFormat sdf =new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        Integer num = 0;
        NjSub njSub = new NjSub();
        njSub.setSuid(suid);
        njSub.setSucheckout(sucheckout);
        Date date = new Date();
        String nuu1=sdf.format(date);
        njSub.setSusurvey(nuu1);
          num = njSubMapper.UpdateNjByIdJY(njSub,sucheckout);
        return num;
    }
}
